import PopconfirmButton from '@/components/BizButtons/PopconfirmButton';
import useActionControl from '@/hooks/useActionControl';
import SystemPostEditModal from '@/pages/system/post/components/SystemPostEditModal';
import ApiSystemPost from '@/services/system/ApiSystemPost';
import { DeleteOutlined, EditOutlined } from '@ant-design/icons';
import { ProTable } from '@ant-design/pro-components';
import { Button, message } from 'antd';
import ButtonGroup from 'antd/es/button/button-group';

const SystemPostIndex = () => {
  const actionControl = useActionControl({
    addAction: {
      onActionCall: (values) => ApiSystemPost.add(values),
    },
    editAction: {
      onModalOpen: (values) => ApiSystemPost.detail(values.postId),
      onActionCall: (values) => ApiSystemPost.edit(values),
    },
  });

  actionControl.queryParams.refill();

  return (
    <div>
      <ProTable
        {...actionControl.table}
        request={async (params: any) => {
          const data = await ApiSystemPost.list(params);
          return {
            data: data.rows,
            success: true,
            total: data.total,
          };
        }}
        toolBarRender={() => []}
        rowKey={'postId'}
        columns={[
          { title: '岗位名称', dataIndex: 'postName' },
          { title: '岗位编码', dataIndex: 'postCode' },
          { title: '岗位排序', dataIndex: 'postSort' },
          { title: '状态', dataIndex: 'status' },
          { title: '备注', dataIndex: 'remark' },
          {
            title: '操作',
            hideInSearch: true,
            render: (text, record) => [
              <ButtonGroup key={'operations'}>
                <Button
                  key="edit"
                  icon={<EditOutlined />}
                  type={'link'}
                  onClick={() => {
                    actionControl.actions.openUpdateModal(record);
                  }}
                >
                  修改
                </Button>
                <PopconfirmButton
                  key={'delete'}
                  buttonText={'删除'}
                  title={'删除后不可恢复, 确认删除吗?'}
                  buttonProps={{
                    danger: true,
                    type: 'link',
                    icon: <DeleteOutlined />,
                  }}
                  onConfirm={async () => {
                    await ApiSystemPost.remove(record.postId);
                    message.success('删除成功');
                    actionControl?.table.actionRef?.current?.reload();
                  }}
                />
              </ButtonGroup>,
            ],
          },
        ]}
      />

      <SystemPostEditModal {...actionControl.editModal} />
    </div>
  );
};

export default SystemPostIndex;
